package com.fwh.web.jdk18newcharacteristic;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * jdk1.8新特性demo
 *
 * @author fwh
 * @date 2019/7/11 下午10:41
 */
@Slf4j
public class Jdk18NewCharacteristic {
    /**
     * 传统和lambda比较写法区别
     *
     * @param
     * @return void
     * @author fwh [2019/7/11 && 下午10:47]
     */
    @Test
    public void lambdaDemo() {
        //传统方式
        List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");
        Collections.sort(names, new Comparator<String>() {
            @Override
            public int compare(String a, String b) {
                return b.compareTo(a);
            }
        });
        System.out.println("传统排序结果为：" + names);

        //lambda表达--1.
//        Collections.sort(names, (String a, String b) -> {
//            return b.compareTo(a);
//        });
//        System.out.println("排序结果为：" + names);

        //lambda表达--2
        names.sort((a, b) -> a.compareTo(b));
        System.out.println("lambda排序结果为：" + names);
    }

    /**
     * map的使用
     */
    @Test
    public void mapsTest() {
        Map<Integer, String> map = new HashMap<>();
//        map.computeIfPresent("3");
//        map.merge()
//        map.getOrDefault()
    }

    /**
     * 获取指定条数的数据
     *
     * @param
     * @return void
     * @author fwh [2019/7/12 && 下午2:37]
     */
    @Test
    public void indexLimitList() {
        List<String> testList = new ArrayList<>();
        testList.add("1");
        testList.add("2");
        testList.add("3");
        testList.add("4");
        testList.add("5");
        testList.add("6");
        testList.add("7");
        int i = 5;
        int limit = 2;
        int size = i + limit;
        final List<String> strings = testList.subList(i, size);
        System.out.println(strings);
    }

    /**
     * 时间API
     *
     * @param
     * @return void
     * @author fwh [2019/7/12 && 下午5:44]
     */
    @Test
    public void clockTest() {
        //
        final Clock clock = Clock.systemDefaultZone();
        final long millis = clock.millis();
        log.info("当前毫秒:{}", millis);

        final Instant instant = clock.instant();
        log.info("当前时间:{}", instant);

        final Date from = Date.from(instant);
        log.info("旧的API时间:{}", from);

        //输出所有区域标识符Timezones(时区)
        System.out.println(ZoneId.getAvailableZoneIds());
        ZoneId zone1 = ZoneId.of("Europe/Berlin");
        ZoneId zone2 = ZoneId.of("Brazil/East");
        System.out.println(zone1.getRules());// ZoneRules[currentStandardOffset=+01:00]
        System.out.println(zone2.getRules());// ZoneRules[currentStandardOffset=-03:00]


        //LocalTime(本地时间)
        LocalTime now1 = LocalTime.now(zone1);
        LocalTime now2 = LocalTime.now(zone2);
        System.out.println(now1.isBefore(now2));  // false
        long hoursBetween = ChronoUnit.HOURS.between(now1, now2);
        long minutesBetween = ChronoUnit.MINUTES.between(now1, now2);
        System.out.println(hoursBetween);       // -3
        System.out.println(minutesBetween);     // -239

        //LocalDate(本地日期)
        LocalDate today = LocalDate.now();//获取现在的日期
        System.out.println("今天的日期: " + today);//2019-03-12
        LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
        System.out.println("明天的日期: " + tomorrow);//2019-03-13
        LocalDate yesterday = tomorrow.minusDays(2);
        System.out.println("昨天的日期: " + yesterday);//2019-03-11
        LocalDate independenceDay = LocalDate.of(2019, Month.MARCH, 12);
        DayOfWeek dayOfWeek = independenceDay.getDayOfWeek();
        System.out.println("今天是周几:" + dayOfWeek);//TUESDAY
    }


    /**
     * 冒泡排序--
     * ^ 表示按位移
     *
     * @return void
     * @author fwh [2019/7/14 && 下午10:08]
     */
    @Test
    public void BubbleSort4() {
//        String stringArray[] = { "1", "2", "3", "4", "5", "6", "7" };
//        int[] test = new int[]{1,2,4,6,9,19,5};
        long[] arr = new long[]{1, 2, 4, 6, 9, 19, 5};
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] > arr[j]) {
                    arr[i] = arr[i] ^ arr[j];
                    arr[j] = arr[i] ^ arr[j];
                    arr[i] = arr[i] ^ arr[j];
                }
            }
        }
        System.out.println(arr.toString());
    }

    @Test
    public void mapTest() {
        Map<String, Object> map = new HashMap(10);
        map.put("1", 1);
        map.put("2", 2);
        map.put("3", 3);

        //方法一：
        final Set set = map.entrySet();
        final Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            final Object next = iterator.next();
//            Map.Entry entry = (Map.Entry) iterator.next();
//            System.out.println(entry.getKey());
//            System.out.println(entry.getValue());
            System.out.println(next.toString());
        }
        System.out.println("===================");
        //方法二：
        map.forEach((key,value)->{
            System.out.println(key);
            System.out.println(value.toString());
        });
        System.out.println("===================");
        //方法三
        for (Map.Entry<String, Object> stringObjectEntry : map.entrySet()) {
            System.out.println(stringObjectEntry.getKey());
            System.out.println(stringObjectEntry.getValue());
        }
        System.out.println("===================");
        final Stream<Map.Entry<String, Object>> stream = map.entrySet().stream();
//        final Optional<Map.Entry<String, Object>> first = stream.findFirst();
//        System.out.println(first);
        map.entrySet().stream()
                .filter(test -> test.getValue().equals("3"))
                .map(test -> test.getValue());
    }

    @Test
    public void mapTest2(){
        Map<Integer, String> HOSTING = new HashMap<>();
        HOSTING.put(1, "linode.com");
        HOSTING.put(2, "heroku.com");
        HOSTING.put(3, "digitalocean.com");
        HOSTING.put(4, "aws.amazon.com");
        String result = "";
        for (Map.Entry<Integer, String> entry : HOSTING.entrySet()) {
            if ("aws.amazon.com".equals(entry.getValue())) {
                result = entry.getValue();
            }
        }
        System.out.println("Before Java 8 : " + result);
        //Map -> Stream -> Filter -> String
        result = HOSTING.entrySet().stream()
                .filter(map -> "aws.amazon.com".equals(map.getValue()))
                .map(map -> map.getValue())
                .collect(Collectors.joining());
        System.out.println("With Java 8 : " + result);
    }

}
